<script type='text/javascript' src='{$REMOTE_ACTION}client'></script>
{literal}
<script type="text/javascript">
	var menu_id = false;
	var report_template_id = false;

	var mr = new menureport();
	mr.Sync();

	var r = new report();

	function updateTemplate(reportSelect) {
		var report_id = reportSelect.value;

		var template_select = document.getElementById('template');

		var templates = r.gettemplatelist(report_id,true);

		template_select.options[0].value=0;
		template_select.options[0].text=" ";
		for(var i =1; i < template_select.length; i++) {
			template_select.remove(i);
		}

		if (templates.length == 0) {
			template_select.options[0].value=0;
			template_select.options[0].text="Select a Report";
			return;
		}

		for(var i =0; i < templates.length; i++) {
			val = templates[i].split('|');
			
			template_select.options[(i+1)] = new Option(val[1],val[0]);
		}
	}

	function setId(el) {
		if (el.name == "report_template_id") {
			report_template_id = el.value;
			document.getElementById('newitem').style.display = "block";
		}
		if (el.name == "menu_id") {
			menu_id = el.value;
			updateMenuItems();
		}
	}

	function updateMenuItems() {
		var items = mr.getmenulist(menu_id,true);

		if (items.length == 0) {
			document.getElementById("updateContent").innerHTML = "No reports for this section";
		} 
		else {
			var content = "<table><tr><th>Menu Title</th><th>Report Name</th><th>Template Name</th><td></td></tr>";
			for(var i =0; i < items.length; i++) {
				val = items[i].split('|');
				content += '<tr><td><input onBlur="updateMenuEntry('+val[0]+',this)" name="item['+val[0]+']" value="'+val[1]+'"></td><td>'+val[3]+
					'</td><td>'+val[4]+ '</td><td><input type="submit" value="delete" onClick="deleteMenuEntry('+ val[0]+",'"+val[1]+"')\"></td></tr>";

			}
			document.getElementById('updateContent').innerHTML = content;
		}

		document.getElementById('add').style.display = 'block';
		document.getElementById('update').style.display = 'block';
	}

	function addMenuEntry() {
		mr.addmenuentry(menu_id,report_template_id,document.getElementById('title').value);
		updateMenuItems();
	}

	function updateMenuEntry(id,el) {
		mr.updatemenuentry(id,el.value);
	}

	function deleteMenuEntry(id,name) {
		if (confirm('Delete Menu Entry: '+name)) {
			mr.deletemenuentry(id);
			updateMenuItems();
		}
	}
</script>
{/literal}
<form name="report" id="h3sForm" method="post" action="{$FORM_ACTION}" enctype="multipart/form-data" onSubmit="return false;">
	<fieldset>
		<fieldset class="radio">
			<legend>Select Menu Section</legend>
			{foreach from=$menu->toDisplayArray() item=menu key=menu_id}
				{if strstr($menu_id,'ss-')}
					{if $notfirst}<br>{/if}
					{assign var=notfirst value=1}
					<b>{$menu}</b><br>
				{else}
				<input onclick="setId(this)" type="radio" name="menu_id" value="{$menu_id}" id="r{$menu_id}"><label class="cb" for="r{$menu_id}">{$menu}</label><br>
				{/if}
			{/foreach}
		</fieldset>

		<fieldset class="radio" id="update" style="display:none">
			<legend>Update Menu Entry</legend>
			<div id="updateContent">
			</div>
		</fieldset>

		<fieldset class="radio" id="add" style="display:none">
			<legend>Add new Report</legend>

			<label class="cb">Select Report and Template</label>
			<select name="report" onChange="updateTemplate(this)">
				{html_options options=$report->getReportList()}
			</select>

			<select name="report_template_id" id="template" onChange="setId(this)">
				<option>Select a Report</option>
			</select>

			<div id="newitem" style="display:none">
				<table>
					<tr>
						<td><label>Title: </label></td><td><input id="title" name="title" type="text" width="20"></td>
						<td><input type="submit" value="Add Menu Entry" onClick="addMenuEntry()"></td>
					</tr>
				</table>
			</div>
		</fieldset>
	</fieldset>
</form>
